package com.bird.pro.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bird.pro.Page;
import com.bird.pro.Params;
import com.bird.pro.mapper.BirdPostMapper;
import com.bird.pro.mapper.BirdPosttypeMapper;
import com.bird.pro.mapper.BirdUserMapper;
import com.bird.pro.mapper.voMapper.PostMapper;
import com.bird.pro.pojo.BirdPost;
import com.bird.pro.pojo.BirdPostExample;
import com.bird.pro.pojo.BirdPostExample.Criteria;
import com.bird.pro.pojo.BirdPosttype;
import com.bird.pro.pojo.BirdUser;
import com.bird.pro.service.IPostService;
import com.bird.pro.vo.PostVo;

@Service
@Transactional
public class PostServiceImpl implements IPostService {

	@Autowired
	private BirdPostMapper postMapper;
	@Autowired
	private BirdPosttypeMapper postTypeMapper;
	@Autowired
	private PostMapper cPostMapper;
	@Autowired
	private BirdUserMapper userMapper;
	@Override
	public List<BirdPost> queryPosts() {
		BirdPostExample example = new BirdPostExample();
		return postMapper.selectByExample(example);
	}

	@Override
	public List<BirdPost> queryPosts(Integer type, String title) {
		BirdPostExample example = new BirdPostExample();
		Criteria criteria = example.createCriteria();
		if (type != null) {
			criteria.andPostTypeEqualTo(type);
		}
		if (StringUtils.isNotBlank(title)) {
			criteria.andPostTitleLike("%" + title + "%");
		}
		example.setOrderByClause("create_time DESC");
		return postMapper.selectByExample(example);
	}

	@Override
	public Page queryPostVos(Params params) {
		HashMap<String, Object> map = new HashMap<>();
		Integer pageSize = params.getPageSize();
		Integer pageNum = params.getPageNum();
		Integer start = (pageNum - 1) * pageSize;
		map.put("title", params.getTitle());
		map.put("type", params.getType());
		map.put("province", params.getProvince());
		map.put("city", params.getCity());
		map.put("county", params.getCounty());
		map.put("pageStart", start);
		map.put("pageSize", pageSize);
		map.put("postStatus", params.getStatus());
		map.put("firstId", params.getFirstId());
		map.put("secondId", params.getSecondId());
		Integer total = cPostMapper.countByMap(map);
		List<BirdPost> posts = cPostMapper.queryPostByMap(map);
		List<PostVo> postVos = new ArrayList<>();
		PostVo post = null;
		BirdPosttype posttype = null;
		BirdUser user = null;
		for(BirdPost p : posts){
			post = new PostVo();
			posttype = postTypeMapper.selectByPrimaryKey(p.getPostType());
			user = userMapper.selectByPrimaryKey(p.getUserId());
			post.setPost(p);
			post.setTypeName(posttype.getTypeName());
			if (StringUtils.isNotBlank(params.getName())) {
				if (user.getUserName().contains(params.getName())) {
					post.setUserName(user.getUserName());
					postVos.add(post);
				}
			}else{
				post.setUserName(user.getUserName());
				postVos.add(post);
			}
		}
		Page page = new Page();
		page.setData(postVos);
		page.setPageNum(pageNum);
		page.setPageSize(pageSize);
		page.setTotal(total);
		return page;
	}
	
	@Override
	public BirdPost getPostById(Integer id) {
		if (id != null) {
			return postMapper.selectByPrimaryKey(id);
		}
		return null;
	}

	@Override
	public int savePost(BirdPost post) {
		if (StringUtils.isNotBlank(post.getPostTitle()) && StringUtils.isNotBlank(post.getPostContent())) {
			post.setCreateTime(new Date());
			return postMapper.insertSelective(post);
		}
		return 0;
	}

	@Override
	public int deletePost(Integer id) {
		if (id != null) {
			return postMapper.deleteByPrimaryKey(id);
		}
		return 0;
	}

	@Override
	public PostVo getPostVoById(Integer id) {
		PostVo postVo = new PostVo();
		if (id != null) {
			BirdPost post = postMapper.selectByPrimaryKey(id);
			BirdPosttype posttype = postTypeMapper.selectByPrimaryKey(post.getPostType());
			BirdUser user = userMapper.selectByPrimaryKey(post.getUserId());
			postVo.setPost(post);
			postVo.setTypeName(posttype.getTypeName());
			postVo.setUserName(user.getUserName());
		}
		return postVo;
	}

	@Override
	public int updatePost(BirdPost post) {
		// TODO Auto-generated method stub
		return postMapper.updateByPrimaryKeySelective(post);
	}

	@Override
	public List<BirdPost> queryStickPosts(String province,String city) {
		BirdPostExample example = new BirdPostExample();
		Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(province)) {
			criteria.andPostProvinceLike(province);
		}
		if (StringUtils.isNotBlank(city)) {
			criteria.andPostCityLike(city);
		}
		criteria.andPostStickEqualTo("1");
		example.setOrderByClause("create_time DESC");
		return postMapper.selectByExampleWithBLOBs(example);
	}

}
